﻿//using RndMaterialDisposalService.Services.Interface;
//using Microsoft.AspNetCore.Mvc.Infrastructure;
//using Microsoft.AspNetCore.Mvc.ModelBinding;
//using Microsoft.AspNetCore.Mvc.Razor;
//using Microsoft.AspNetCore.Mvc.RazorPages;
//using Microsoft.AspNetCore.Mvc.Rendering;
//using Microsoft.AspNetCore.Mvc.ViewFeatures;
//using Microsoft.AspNetCore.Mvc;
//using System.Diagnostics;
//using System.Text.Encodings.Web;
//using RndMaterialDisposalService.Attrbutes;

//namespace RndMaterialDisposalService.Services
//{
//    /// <summary>
//    /// 
//    /// </summary>
//    [NAutowired]
//    public class ViewRenderService : IViewRenderService
//    {
//        private readonly IRazorViewEngine _razorViewEngine;
//        private readonly ITempDataProvider _tempDataProvider;
//        private readonly IHttpContextAccessor _httpContext;
//        private readonly IActionContextAccessor _actionContext;
//        private readonly IRazorPageActivator _activator;

//        /// <summary>
//        /// 
//        /// </summary>
//        /// <param name="razorViewEngine"></param>
//        /// <param name="tempDataProvider"></param>
//        /// <param name="httpContext"></param>
//        /// <param name="activator"></param>
//        /// <param name="actionContext"></param>
//        public ViewRenderService(IRazorViewEngine razorViewEngine,
//            ITempDataProvider tempDataProvider,
//            IHttpContextAccessor httpContext,
//            IRazorPageActivator activator,
//            IActionContextAccessor actionContext)
//        {
//            _razorViewEngine = razorViewEngine;
//            _tempDataProvider = tempDataProvider;

//            _httpContext = httpContext;
//            _actionContext = actionContext;
//            _activator = activator;

//        }

//        /// <summary>
//        /// 
//        /// </summary>
//        /// <typeparam name="T"></typeparam>
//        /// <param name="pageName"></param>
//        /// <param name="model"></param>
//        /// <returns></returns>
//        /// <exception cref="ArgumentNullException"></exception>
//        public async Task<string> RenderToStringAsync<T>(string pageName, T model) where T : PageModel
//        {
//            var actionContext = new ActionContext( _httpContext.HttpContext,_httpContext.HttpContext.GetRouteData(),_actionContext.ActionContext.ActionDescriptor);

//            using (var sw = new StringWriter())
//            {
//                var result = _razorViewEngine.FindPage(actionContext, pageName);

//                //var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);

//                var result22 = _razorViewEngine.GetAbsolutePath(pageName,"");

//                if (result.Page == null)
//                {
//                    throw new ArgumentNullException($"The page {pageName} cannot be found.");
//                }

//                var view = new RazorView(_razorViewEngine,
//                    _activator,
//                    new List<IRazorPage>(),
//                    result.Page,
//                    HtmlEncoder.Default,
//                    new DiagnosticListener("ViewRenderService"));

//                var viewContext = new ViewContext(actionContext,view,
//                    new ViewDataDictionary<T>(new EmptyModelMetadataProvider(), new ModelStateDictionary())
//                    {
//                        Model = model
//                    },
//                    new TempDataDictionary(
//                        _httpContext.HttpContext,
//                        _tempDataProvider
//                    ),
//                    sw,
//                    new HtmlHelperOptions()
//                );

//                var page = ((Page)result.Page);
//                page.PageContext = new Microsoft.AspNetCore.Mvc.RazorPages.PageContext
//                {
//                    ViewData = viewContext.ViewData
//                };
//                page.ViewContext = viewContext;
//                _activator.Activate(page, viewContext);
//                await page.ExecuteAsync();
//                return sw.ToString();
//            }
//        }

//    }
//}
